package com.ebanma.cloud.usertestall.controller;

import com.ebanma.cloud.usertestall.domain.common.ErrorCode;
import com.ebanma.cloud.usertestall.domain.common.Result;
import com.ebanma.cloud.usertestall.domain.dto.UserDTO;
import com.ebanma.cloud.usertestall.domain.entity.UserPO;
import com.ebanma.cloud.usertestall.service.UserService;
import com.ebanma.cloud.usertestall.utils.DaoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/api/user")
public class UserController {
     @Autowired
     private UserService userService;
    /**
     * POST /api/user
     * 新增用户
     */
     @PostMapping
     public Result save(@RequestBody UserDTO userDTO) {

         int save = userService.save(userDTO);

         if (save == 1) {
             return Result.success();
         } else {
             return Result.fail(ErrorCode.SYSTEM_ERROR);
         }

     }
    /**
     * PUT /api/user/{id}
     * 更新用户信息
     */
    @PutMapping
    public Result update(@PathVariable("id") Long id, @RequestBody UserDTO userDTO) {



        int update = userService.update(id, userDTO);


        if (update == 1) {
            return Result.success();
        } else {
            return Result.fail(ErrorCode.SYSTEM_ERROR);
        }

    }














    @PostMapping
    public Result selectByExecute(@RequestBody UserDTO userDTO) {
        UserPO po = DaoUtils.execute(sqlSession -> {
            UserPO userPO = new UserPO();
            return (UserPO)sqlSession.selectOne("", userPO);
        });
        return  Result.success(po);

    }


}
